#include "Game.h"
// globals
#include <d3d9.h>
LPDIRECT3D9       g_pDirect3D = NULL;
LPDIRECT3DDEVICE9 g_pDirect3D_Device = NULL;
//--------------------------------------------------------------------------------
Game::Game(HINSTANCE hInstance)
:
m_pkWindows(NULL),
m_hInstance(hInstance)
{

}
//--------------------------------------------------------------------------------
Game::~Game()
{

}
//--------------------------------------------------------------------------------
bool Game::StartUp()
{
	//variable temporal para almacenar el hWnd recibirlo del createWindow y 
	//pasarlo a InitDX
	HWND hWnd=NULL;

	m_pkWindows = new Window(m_hInstance);

	if (!m_pkWindows)
		return false;

	m_pkWindows->createWindow(800, 600, hWnd);

	//----------------------------------------------------------
	/* Inicializar DX pasar a renderer()
	
	g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);

	D3DPRESENT_PARAMETERS PresentParams;
	memset(&PresentParams, 0, sizeof(D3DPRESENT_PARAMETERS));

	PresentParams.Windowed = TRUE;
	PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;


	g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,
								D3DDEVTYPE_HAL,
								hWnd,
								D3DCREATE_SOFTWARE_COL_VERTEXPROCESSING,
								&PresentParams,
								&g_pDirect3D_Device);

	*/----------------------------------------------------------

	render = new Renderer(hWnd);

	//struct D3DCOL_VERTEX {float x, y, z, rhw; DWORD color;} vertices[3];
	
	/*vertices = new COL_VERTEX[3];
	vertices[0].x = 50; 
	vertices[0].y = 50; 
	vertices[0].z = 0; 
	vertices[0].rhw = 1.0f; 
	vertices[0].color = 0x00ff00;

	vertices[1].x = 250; 
	vertices[1].y = 50; 
	vertices[1].z = 0; 
	vertices[1].rhw = 1.0f; 
	vertices[1].color = 0x0000ff;

	vertices[2].x = 50; 
	vertices[2].y = 250; 
	vertices[2].z = 0; 
	vertices[2].rhw = 1.0f;
	vertices[2].color = 0xff0000;/**/

	
	
	//vertices->resetVertexs();
	/**/

	if (!OnStartUp())
		return false;

	return true;
}
//--------------------------------------------------------------------------------
bool Game::Loop()
{
	/*g_pDirect3D_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255),

                          1.0f, 0);
	// render the actual scene
	if(SUCCEEDED(g_pDirect3D_Device->BeginScene()))
	{
		g_pDirect3D_Device->SetStreamSource(0, pCOL_VERTEXObject, 0, sizeof(D3DCOL_VERTEX));
		g_pDirect3D_Device->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE);
		g_pDirect3D_Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
		g_pDirect3D_Device->EndScene();
	}


	g_pDirect3D_Device->Present(NULL, NULL, NULL, NULL);
	*/
	
	render->StartFrame();
	
	//render->Draw(vertices, 3, DrawPrimitives::TRIANGLE_LIST);

	//ValidateRect(hW, NULL);

	if (OnLoop())
		return true;
	
	render->EndFrame();

	return false;
}
//--------------------------------------------------------------------------------
bool Game::ShutDown()
{
	if (!OnShutDown())
		return false;

	if (m_pkWindows)
		delete m_pkWindows;

	m_pkWindows = NULL;

	delete render;

	return true;
}
//--------------------------------------------------------------------------------
